package zt.song.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import zt.song.common.Const;
import zt.song.domain.User;

/**
 * @Author 宋伟宁
 * @Date 2023/12/11
 * @Version 1.0
 **/
@Controller
public class HelloController extends BaseController{

    @GetMapping("/")
    public String tologin(){
        return "login";
    }

    @GetMapping("/mlogin")
    public String tomlogin(){
        return "mlogin";
    }


    @PostMapping("/mlogin")
    public String mlogin(String mobile,String code,Model model, HttpSession session){
        //获取验证码
        String s = this.redisTemplate.opsForValue().get(mobile + "_" + code);
        System.out.println("redis的验证码："+s);
        if(s.equalsIgnoreCase(code)){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mobile",mobile);
            User user = this.userService.getOne(queryWrapper);
            if(user!=null){
                user.setAuths(this.userService.findAuths( user.getUid()));
                session.setAttribute("user",user);
                return "index";
            }else{
                model.addAttribute("msg","<font color='red'>手机号码不存在！</font>");
                return "mlogin";
            }
        }else{
            // 验证码错误
            model.addAttribute("msg","<font color='red'>验证码错误！</font>");
            return "mlogin";
        }

    }

    @GetMapping("/index")
    public String index(Model model){
        //model.addAttribute(user);
        return "index";
    }

    @PostMapping("/login")
    public String login(String username, String password, Model model, HttpSession session){
        //验证码
//        String code =(String) this.redisUtil.get(Const.CAPTCHA_KEY);
//        if(code.equalsIgnoreCase(chkcode)){
              //验证账号密码
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",username)
                    .eq("password", password);
            User user = this.userService.getOne(queryWrapper);
            if(null!=user){
                 //获取用户的菜单
                 user.setAuths(this.userService.findAuths( user.getUid()));
                 session.setAttribute("user",user);
                 return "index";
            }else{
                model.addAttribute("msg","<font color='red'>用户名或密码错误");
                return "login";
            }

    }

//    @GetMapping("/test")
//    @Scheduled(cron = "0/2 * * * * ?")
//    public String test() {
//        System.out.println(System.currentTimeMillis()+"hello");
//        return "hello";
//    }
}
